package znbkTestPaperBj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.example.lancooznbk.view.widget.LoadingDialog;
import com.example.lancooznbk.view.widget.ScaleButton;

import lancooControl.AndroidBug5497Workaround;
import lancooControl.CustomEditText;
import znbkTestPaperBj.ZnbkPaperBjInfor;
import znbkTestPaperBj.entity.NotePaperEntity;
import znbkChangeNote.presenter.DeleteNotePresenter;
import znbkChangeNote.presenter.SaveNotePresenter;
import znbkListBj.entity.NoteListEntity;
import znbkTestPaperBj.presenter.NotePaperPresenter;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperSc.activity.CollectionPaperActivity;

/**
 * Created by Administrator on 2018\3\22 0022.
 */

public class NotePaperActivity extends BaseActivity implements ILoadPaperView<NotePaperEntity> {

    CustomEditText CEdtNotes;
    private ScaleButton mSBtnBack;//返回按钮
    private TextView mTvTittle;
    private ScaleButton mSBtnMore;
    private ScaleButton mSBtnOriginalQuestion;

    private NoteListEntity.NoteBean mEntity;
    public static final  String PAPER_FROM = "paperFrom";
    public static final  String PAPER_ENTITY = "paperEntity";
    public static int mQuesOrPaperFrom = -1;

    private NotePaperPresenter mPresenter;
    public static NotePaperEntity mEntityBj;

    public  static void launch(BaseFragment mContext, NoteListEntity.NoteBean entity, @ZnbkPaperDef.PaperFrom int quesOrPaperFrom){

        if(entity.getResType().equals("4")||
                entity.getResType().equals("6")){
            ZnbkPaperBjInfor.IsFromSpl = true;
        }else{
            ZnbkPaperBjInfor.IsFromSpl = false;
        }

        ZnbkPaperBjInfor.QuesID= entity.getQuesID();
        ZnbkPaperBjInfor.ResType = entity.getResType();
        ZnbkPaperBjInfor.NoteID = entity.getNoteID();
        ZnbkPaperBjInfor.Title = entity.getTitle();
        ZnbkPaperBjInfor.Content = entity.getContent();

        if(entity.getResType().equals("1")||
                entity.getResType().equals("2")||
                entity.getResType().equals("3")||
                entity.getResType().equals("4")||
                entity.getResType().equals("5")||
                entity.getResType().equals("6")){

            ZnbkPaperBjInfor.Flag = 1;
        }else {
            ZnbkPaperBjInfor.Flag = 0;
        }
        ZnbkPaperBjInfor.Content = entity.getContent();

        mQuesOrPaperFrom = quesOrPaperFrom;
        Bundle bundle = new Bundle();
        bundle.putSerializable(PAPER_ENTITY,entity);
        bundle.putInt(PAPER_FROM,quesOrPaperFrom);
        Intent intent = new Intent(mContext.getContext(), NotePaperActivity.class);
        intent.putExtras(bundle);
        mContext.startActivityForResult(intent,1);
    }


    @Override
    public int attachLayoutRes() {
        return R.layout.znbk_layout_add_notes_view;
    }

    @Override
    protected void initViews() {
        AndroidBug5497Workaround.assistActivity(this);
        mPresenter = new NotePaperPresenter(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            mEntity = (NoteListEntity.NoteBean) bundle.getSerializable(PAPER_ENTITY);
            mQuesOrPaperFrom = bundle.getInt(PAPER_FROM);
        }

        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        /*
        * 返回按钮菜单相关设置
        * */
        mSBtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!ZnbkPaperBjInfor.Content.equals("")){
                    saveNote();
                }else{
                    deleteNote();
                }
                HideKeyboard();
                finish();
            }
        });


        /*
        * 页面头部-》笔记详情：mTvTittle
        * */
        mTvTittle = (TextView) findViewById(R.id.tv_title);
        mTvTittle.setText(R.string.znbk_titleNoteDetails);

        /*
        * 更多功能按钮菜单相关信息
        * */
        mSBtnMore = (ScaleButton) findViewById(R.id.sbtn_more);
        mSBtnMore.setVisibility(View.GONE);


        CEdtNotes = (CustomEditText) findViewById(R.id.cedt_notes);
        CEdtNotes.setText(ZnbkPaperBjInfor.Content);
        CEdtNotes.setSelection(ZnbkPaperBjInfor.Content.length());
        CEdtNotes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                ZnbkPaperBjInfor.Content = s.toString();
            }
        });

        mSBtnOriginalQuestion = (ScaleButton) findViewById(R.id.sbtn_CheckOriginalQuestion);
        if(mQuesOrPaperFrom == ZnbkPaperDef.FROM_NOTE){
            mSBtnOriginalQuestion.setVisibility(View.VISIBLE);
        }else{
            mSBtnOriginalQuestion.setVisibility(View.GONE);
        }
        mSBtnOriginalQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ZnbkPaperBjInfor.IsFromSpl){
                    NoteCheckOriginalQuestionPaperSplActivity.launch(NotePaperActivity.this, ZnbkPaperDef.FROM_NOTE);
                }else {
                    NoteCheckOriginalQuestionPaperActivity.launch(NotePaperActivity.this, ZnbkPaperDef.FROM_NOTE);
                }
            }
        });


    }

    /*
    * 保存笔记
    * */
    SaveNotePresenter mSaveNotePresenter;
    private void saveNote(){
        mSaveNotePresenter = new SaveNotePresenter(this,mQuesOrPaperFrom);
        mSaveNotePresenter.getData(false);
    }

    /*
    * 删除笔记
    * */
    DeleteNotePresenter mDeleteNotePresenter;
    private void deleteNote(){
        mDeleteNotePresenter = new DeleteNotePresenter(this,mQuesOrPaperFrom);
        mDeleteNotePresenter.getData(false);
    }


    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }


    @Override
    public void loadPaperData(NotePaperEntity data) {
        mEntityBj = data;
    }

    @Override
    public void loadComplete(boolean hasComplete) {

    }

    @Override
    public void openExamReportDialog() {

    }

    @Override
    public void openAssessDialog() {

    }

    @Override
    public void updatingResult(boolean isSucceed) {

    }

    @Override
    public void updatingReportResult(boolean isReportSucceed) {

    }

    @Override
    public LoadingDialog getLoadingDialog() {
        return null;
    }

    //隐藏虚拟键盘:Fragment进行切换的时候使用
    public  void HideKeyboard() {
        /**隐藏软键盘**/
        View viewK = this.getWindow().peekDecorView();
        if(viewK != null){
            InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(viewK.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*
    * 监听系统home事件
    * */
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }

    /*
    * 监听系统返回事件
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            return false;
        }
        return false;
    }

}
